﻿//using NuGet.Protocol.Core.Types;
using Microsoft.EntityFrameworkCore;
using NuGet.Protocol.Core.Types;
using System;
using System.Collections.Generic;
using System.IO.Pipes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MYData.Core.BaseProvider;
using MYData.Entity.DomainModels;
using MYData.Entity.DomainModels.ApiEntity;
using MYData.Entity.Project;
using MYData.System.IRepositories.System;


namespace MYData.System.Services.System
{
    public partial class Tb_DepartService
    {
        //private static List<Tb_Project_Department> _depart { get; set; }
        public async Task<object> DepartGetList()
        {
            //var parentId = "0";
            var tbdepart = (repository.FindAsIQueryable(x => x.id != null).Select(x => new Department_Dto
            {
                Id = x.id,
                value = x.departmentName,
                label = x.departmentName,
                Num = x.departmentNum,
                Fid = x.parentId,
            }).ToList());
            return tbdepart;
        }
        /// <summary>
        /// 部门递归
        /// </summary>
        /// <returns></returns>
        public async Task<object> DepartShow()
        {
            //var parentId = "0";
            var tbdepart = (await repository.FindAsync(x => x.parentId == "0")).Select(x => new Ditem
            {
                Id = x.id,
                value = x.departmentName,
                label = x.departmentName,
                //Num = x.departmentNum,
                //Fid = x.parentId,
                children = DepartShowPlan(x.id),
            }).ToList();
            return tbdepart;
        }
        /// <summary>
        /// 递归方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private List<Ditem> DepartShowPlan(string id)
        {
            //var tbdepart = (await repository.FindAsync(x => x.parentId == parentId));
            var tbDepart = (repository.Find(x => x.parentId == id)).Select(x => new Ditem
            {
                Id = x.id,
                value = x.departmentName,
                label = x.departmentName,
                //Num = x.departmentNum,
                //Fid = x.parentId,
                children = DepartShowPlan(x.id),
                //Chi = null,
            }).ToList();
            return tbDepart;
        }
        /// <summary>
        /// 根据ID查部门
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<object> DepartSearch(string id)
        {
            //var parentId = "0";
            var tbDepart = (await repository.FindAsync(x => x.id == id)).ToList();
            return tbDepart;
        }
        /// <summary>
        /// 根据部门Id获取承担单位信息
        /// MJZ 2023/3/29/16:29
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Tb_Project_Department GetResponsibleUnit(string id)
        {
            Tb_Project_Department parent=new Tb_Project_Department();
            while (true)
            {
                parent = (repository.Find(x => x.id.Equals(id))).FirstOrDefault();
                if (parent.parentId=="0")
                {
                    return parent;
                }
                id = parent.parentId;
            }
            return parent;
        }
    }
}
